package com.xuhai.blog.common.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.xuhai.blog.admin.controller.AbstractController;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author PangJunjie
 * @Date 2022/2/9/009
 */
@Component
public class MyMetaObjectHandler extends AbstractController implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Object createTime=getFieldValByName("createTime",metaObject);
        Object createAdmin=getFieldValByName("createAdmin",metaObject);
        if(createTime == null){
            setFieldValByName("createTime",new Date(),metaObject);
        }
        if(createAdmin == null){
            setFieldValByName("createAdmin",getUserId(),metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Object updateTime = getFieldValByName("updateTime",metaObject);
        Object updateAdmin = getFieldValByName("updateAdmin",metaObject);
        if(updateTime == null){
            setFieldValByName("updateTime",new Date(),metaObject);
        }
        if(updateAdmin == null){
            setFieldValByName("updateAdmin",getUserId(),metaObject);
        }
    }
}
